﻿using System;
using System.Collections.Generic;

namespace ETW.EventListeners.ObjectValues
{
    /// <summary>
    /// 平均类型计数器有效负载
    /// </summary>
    public class MeanCounterPayload
    {
        /// <summary>
        /// 负载名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 显示名称
        /// </summary>
        public string DisplayName { get; set; }
        /// <summary>
        /// 平均值
        /// </summary>
        public double? Mean { get; set; }
        /// <summary>
        /// 标准差
        /// </summary>
        public double StandardDeviation { get; set; }
        /// <summary>
        /// 计数
        /// </summary>
        public int Count { get; set; }
        /// <summary>
        /// 最小值
        /// </summary>
        public double? Min { get; set; }
        /// <summary>
        /// 最大值
        /// </summary>
        public double? Max { get; set; }
        /// <summary>
        /// 间隔秒
        /// </summary>
        public float IntervalSec { get; set; }
        /// <summary>
        /// 系列
        /// <para>"Interval=5000"</para>
        /// </summary>
        public string Series { get; set; }
        /// <summary>
        /// 计数器类型
        /// <para>
        /// Mean
        /// </para>
        /// </summary>
        public string CounterType { get; set; }
        /// <summary>
        /// 元数据
        /// </summary>
        public string Metadata { get; set; }
        /// <summary>
        /// 显示单位
        /// </summary>
        public string DisplayUnits { get; set; }

        /// <summary>
        /// 转换成平均类型计数器有效负载
        /// </summary>
        /// <param name="eventPayload"></param>
        /// <returns></returns>
        public static MeanCounterPayload ConvertFrom(IDictionary<string, object> eventPayload)
        {
            if (!eventPayload["CounterType"].Equals("Mean"))
                return null;

            return new MeanCounterPayload
            {
                Name = eventPayload["Name"].ToString(),
                DisplayName = eventPayload["DisplayName"].ToString(),
                Mean = double.TryParse(eventPayload["Mean"].ToString(), out var db) ? db : null,
                StandardDeviation = double.TryParse(eventPayload["StandardDeviation"].ToString(), out db) ? db : 0,
                Count = int.TryParse(eventPayload["Count"].ToString(), out var i) ? i : 0,
                Min = double.TryParse(eventPayload["Min"].ToString(), out db) ? db : null,
                Max = double.TryParse(eventPayload["Max"].ToString(), out db) ? db : null,
                IntervalSec = float.TryParse(eventPayload["IntervalSec"].ToString(), out var fl) ? fl : 0,
                Series = eventPayload["Series"].ToString(),
                CounterType = "Mean",
                Metadata = eventPayload["Metadata"].ToString(),
                DisplayUnits = eventPayload["DisplayUnits"].ToString(),
            };
        }
    }
}
